package com.haoshuang.zhouzhoubang.ui.page.shop

import android.content.ContentValues.TAG
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.flyco.tablayout.SlidingTabLayout
import com.haoshuang.zhouzhoubang.databinding.ActivityShoppingBinding
import com.haoshuang.zhouzhoubang.model.bean.ShopSort
import com.haoshuang.zhouzhoubang.ui.adapter.ViewPagerAdapter
import com.haoshuang.zhouzhoubang.ui.page.shop.car.ShopCarActivity
import com.haoshuang.zhouzhoubang.ui.page.shop.child.ShoppingChildFragment
import com.haoshuang.zhouzhoubang.ui.page.shop.order.ShopOrderActivity
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger

/**
 *  商城界面
 */
class ShoppingActivity : BaseVMActivity<ShoppingViewModel, ActivityShoppingBinding>() {
    private val mFragments = ArrayList<Fragment>()  //存放登录方式界面的Fragment集合
    private val mTitles = ArrayList<ShopSort>()   //存放标题文字内容集合
    private var mTabLayout: SlidingTabLayout? = null
    private lateinit var vp: ViewPager
    private var mAdapter: MyPagerAdapter3? = null

    override fun initView(savedInstanceState: Bundle?) {
        binding.appBar.ivBack.clickWithTrigger { finish() }
        binding.appBar.tvTitle.text = "商城"
        // 点击查看购物车
        binding.tvShoppingCart.clickWithTrigger {
            startPage<ShopCarActivity>()
        }
        vp = binding.vpShopping
        mTabLayout = binding.icTitle.slidingTabLayout
        vm.getData2()

        binding.tvOrderCenter.clickWithTrigger {
            // 订单中心
            startPage<ShopOrderActivity>()
        }

    }

    private fun setOnPageChangeListener() {
        vp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                var fam = mFragments[position] as ShoppingChildFragment
                fam.updata()
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
    }

    override fun initData() {
    }

    override fun startObserve() {
        vm.getData2.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code != "402") {
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                if (it != null) {
                    mTitles.addAll(it)
                }

                for (index in mTitles.indices) {
                    val fm = ShoppingChildFragment.newInstance(mTitles[index].id)
                    mFragments.add(fm)

                }

                mAdapter = MyPagerAdapter3(supportFragmentManager, mFragments, mTitles)

                vp.adapter = mAdapter
                val list = arrayListOf<String>()
                for (b in mTitles) {
                    list.add(b.cate_name)
                }
                mTabLayout?.setViewPager(vp, list.toTypedArray())
                // 此处将其放置顶层即可，若是放到下面将会导致重复
                vp.offscreenPageLimit = mTitles.size
                vp.currentItem = 0  //设置初始化时显示的位置
                setOnPageChangeListener()
            }
        }
    }

}